package com.yunhe.dtu.task.controller;

import com.yunhe.common.model.ResultObject;
import com.yunhe.dtu.task.task.DtuDataTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DtuController
 * @Description 定时获取DTU数据
 * @Author HeTao
 * @Date 2020/11/16 13:33
 * @Version 1.0
 **/
@Api(value = "dtuTask",description = "定时获取DTU数据",tags = "{Dtu_Task}")
@RestController
@RequestMapping(value = "/dtuTask")
public class DtuController {

    private static Logger log = LoggerFactory.getLogger(DtuController.class);

    @Autowired
    private DtuDataTask dtuDataTask;

    @ApiOperation("定时获得DTU实时数据")
    @RequestMapping(method = RequestMethod.POST)
    @Scheduled(cron = "0 0/1 * * * ? ")
    @Async
    public ResponseEntity<ResultObject> getDtuRealTimeCollectData() throws InterruptedException {
        log.info("定时获得DTU实时数据定时任务开始");
        //获取四大园区不是总表的数据
        dtuDataTask.getDtuRealTimeCollectData();
        //获取四大园区总表的数据
        dtuDataTask.getTotalRealTimeCollectData();
        log.info("定时获得DTU实时数据定时任务结束");
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }



    @ApiOperation("通过key查询实时值")
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getDtuRealTimeCollectData(@RequestParam(value = "key") String key ){

        return new ResponseEntity<>(new ResultObject<>(dtuDataTask.query(key)), HttpStatus.OK);
    }

    @ApiOperation("校验数据")
    @RequestMapping(value = "/queryData",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> queryData(@RequestParam(value = "sid") String sid ){
        return new ResponseEntity<>(new ResultObject<>(dtuDataTask.queryData(sid)), HttpStatus.OK);
    }
}
